package com.sgb.kjwl.view.ui.activity.self.packers;

import android.util.Log;
import android.view.View;

import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityLaborServiceAndEmploymentPublishListDetailBinding;
import com.sgb.kjwl.model.entity.packers.LabourServiceAndEmploymentEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.NumberConstant;
import com.sgb.kjwl.utils.WebJsConstants;
import com.sgb.kjwl.viewmodel.packers.LaborServiceAndEmploymentPublishListDetailViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.ycbjie.webviewlib.base.X5WebChromeClient;

import org.json.JSONObject;

/**
 * @author : younghow
 * @date : 2021/7/8 16:01
 * @desc :个人中心-劳务用工发布列表的详情页
 * first:写在前面，当我们的分支个数在五个以内，请使用if
 */
public class LaborServiceAndEmploymentPublishListDetailActivity extends AppActivity {

    ActivityLaborServiceAndEmploymentPublishListDetailBinding mBinding;
    LaborServiceAndEmploymentPublishListDetailViewModel mViewModel;
    private LabourServiceAndEmploymentEntity no;
    private X5WebChromeClient chromeClient;
    private boolean mIsSuccess;

    @Override
    protected void initView() {
        super.initView();
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_labor_service_and_employment_publish_list_detail);
        mBinding.setLifecycleOwner(this);
        mViewModel = new ViewModelProvider(this).get(LaborServiceAndEmploymentPublishListDetailViewModel.class);

        no = (LabourServiceAndEmploymentEntity) getIntent().getSerializableExtra("no");

        mBinding.tvLaborServiceDelete.setOnClickListener(this);
        mBinding.tvLaborServiceEdit.setOnClickListener(this);

        initWebView();
        initLiveData();
    }

    private void initLiveData() {
        mViewModel.loadingLiveData.observe(this, aBoolean -> DialogHelper.showProgressDialog(LaborServiceAndEmploymentPublishListDetailActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false));
        mViewModel.successLiveData.observe(this, aBoolean -> DialogHelper.dismissProgressDialog());
        mViewModel.errorLiveData.observe(this, aBoolean -> DialogHelper.dismissProgressDialog());

        mViewModel.mUrlLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                mBinding.wvLaborService.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                mBinding.wvLaborService.getSettings().setDomStorageEnabled(true);
                mBinding.wvLaborService.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
                mBinding.wvLaborService.getSettings().setAppCacheEnabled(true);
                chromeClient = new X5WebChromeClient(mBinding.wvLaborService, LaborServiceAndEmploymentPublishListDetailActivity.this) {
                    @Override
                    public void onProgressChanged(WebView view, int newProgress) {
                        super.onProgressChanged(view, newProgress);
                        Log.e("WebViewX5Activity", newProgress + "");
                    }
                };
                mBinding.wvLaborService.setWebChromeClient(chromeClient);
                mBinding.wvLaborService.loadUrl(s);
            }
        });

        mViewModel.mDeleteSuccessLiveData.observe(this, aBoolean -> {
            if (aBoolean){
                mIsSuccess = true;
            }
            onBackPressed();
        });
    }

    private void initWebView() {
        mViewModel.getH5Url(no.getNo());
        mBinding.wvLaborService.registerHandler(WebJsConstants.GO_TO_CALL_PHONE, (data, function) -> com.sgb.kjwl.utils.DialogHelper.showDialog(getActivity(), "确认呼叫", jsonChange(data), "确定", new DialogFunction() {
            @Override
            public void normalRoundDialog() {
                ActivityJump.jumpToPhone(LaborServiceAndEmploymentPublishListDetailActivity.this, jsonChange(data));
            }
        }));
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int id = v.getId();
        if (id == R.id.tv_labor_service_delete) {
            com.sgb.kjwl.utils.DialogHelper.showDialog(LaborServiceAndEmploymentPublishListDetailActivity.this, "温馨提示", "确定删除该劳务用工信息吗？", "取消", "确定", new DialogFunction() {
                @Override
                public void normalRoundDialog() {
                    mViewModel.deleteLabour(no.getNo());
                }
            });
        } else if (id == R.id.tv_labor_service_edit) {
            ActivityJump.jumpToActivity(LaborServiceAndEmploymentPublishListDetailActivity.this, LaborServiceAndEmploymentPublishActivity.class, Pair.create("entity", no));
        }
    }

    @Override
    public void onBackPressed() {
        if (mIsSuccess) {
            setResult(1);
        }
        super.onBackPressed();
    }

    private String jsonChange(String jsonString){
        String phone = "";
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            phone = jsonObject.getString("id");
        } catch (Exception e) {
            e.fillInStackTrace();
        }

        return phone;
    }
}
